package Test;

import com.sun.jmx.mbeanserver.JmxMBeanServer;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PopupMenuTest {
    private static void PopupTest(){
        JFrame frame = new JFrame("弹出菜单");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,400);
        frame.setLocation(300,200);
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem item1 = new JMenuItem("查看");
        JMenuItem item2 = new JMenuItem("刷新");
        popupMenu.add(item1);
        popupMenu.addSeparator();
        popupMenu.add(item2);
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton()==MouseEvent.BUTTON3){
                    popupMenu.show(e.getComponent(),e.getX(),e.getY());
                }
            }
        });
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(PopupMenuTest::PopupTest);
    }
}
